package com.xuecheng.test.rabbitmq;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer03_topics {
    private static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
    private static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    private static final String EXCHANGE_TOPIC_INFORM="exchange_topic_inform";
    private static final String EXCHANGE_TOPIC_EMAIL="inform.#.email.#";
    private static final String EXCHANGE_TOPIC_SMS="inform.#.sms.#";
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection conn=null;
        Channel channel=null;
        //创建连接工程
        try {
            ConnectionFactory factory=new ConnectionFactory();
            factory.setHost("localhost");
            factory.setPort(5672);
            factory.setUsername("guest");
            factory.setPassword("guest");
            //设置虚拟机
            factory.setVirtualHost("/");
            //建立到代理服务器到连接
            conn = factory.newConnection();
            //获得信道
            channel = conn.createChannel();
            /**
             * 声明队列，如果Rabbit中没有此队列将自动创建
             * param1:队列名称
             * param2:是否持久化
             * param3:队列是否独占此连接 connection连接关闭则自动删除队列 若设置true可用于设置临时队列，rabbit重启则队列删除
             * param4:队列不再使用时是否自动删除此队列 与参数3一起使用用于创建临时队列
             * param5:队列参数 设置其他扩展参数
             */
            channel.queueDeclare(QUEUE_INFORM_EMAIL,true,false,false,null);
            channel.queueDeclare(QUEUE_INFORM_SMS,true,false,false,null);

            /**
             * 定义交换机类型
             * 参数明细
             * 1、交换机名称
             * 2、交换机类型，fanout、topic、direct、headers
             */
            channel.exchangeDeclare(EXCHANGE_TOPIC_INFORM, BuiltinExchangeType.TOPIC);

            //交换机和队列绑定String queue, String exchange, String routingKey
            /**
             * 参数明细
             * 1、队列名称
             * 2、交换机名称
             * 3、路由key ""代表默认
             * 可绑定多个routeKey
             */
            channel.queueBind(QUEUE_INFORM_EMAIL,EXCHANGE_TOPIC_INFORM,EXCHANGE_TOPIC_EMAIL);
//            channel.queueBind(QUEUE_INFORM_EMAIL,EXCHANGE_TOPIC_INFORM,"inform");
            channel.queueBind(QUEUE_INFORM_SMS,EXCHANGE_TOPIC_INFORM,EXCHANGE_TOPIC_SMS);
//            channel.queueBind(QUEUE_INFORM_SMS,EXCHANGE_TOPIC_INFORM,"inform");


            /**
             * 消息发布方法
             * param1：Exchange的名称，如果没有指定，则使用Default Exchange
             * param2:routingKey,消息的路由Key，是用于Exchange（交换机）将消息转发到指定的消息队列
             * param3:消息包含的属性
             * param4：消息体
             * 这里没有指定交换机，消息将发送给默认交换机，每个队列也会绑定那个默认的交换机，但是不能显
             示绑定或解除绑定
             * 默认的交换机，routingKey等于队列名称
             */

            for (int i = 0; i < 5; i++) {
                String message="email info message to user:"+i;
                channel.basicPublish(EXCHANGE_TOPIC_INFORM,"inform.email",null,message.getBytes());
                System.out.println("Send Message is:'" + message + "'");
            }

            for (int i = 0; i < 5; i++) {
                String message="sms info message to user:"+i;
                channel.basicPublish(EXCHANGE_TOPIC_INFORM,"inform.sms",null,message.getBytes());
                System.out.println("Send Message is:'" + message + "'");
            }


            for (int i = 0; i < 5; i++) {
                String message="send info message to user:"+i;
                channel.basicPublish(EXCHANGE_TOPIC_INFORM,"inform.email.sms",null,message.getBytes());
                System.out.println("Send Message is:'" + message + "'");
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //先关闭通道，再关闭连接
            if (channel!=null){
                channel.close();
            }
            if (conn!=null){
                conn.close();
            }


        }

    }
}
